package com.jboa.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.jboa.model.Employee;
import com.jboa.model.Role;
import com.jboa.service.EmployeeBiz;
import com.jboa.service.RoleService;
import com.jboa.util.MD5;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;



public class EmployeeAction extends ActionSupport{

	
	private EmployeeBiz employeeBiz;
	private Employee employee;
	private String errorMsg;
	private String verify;//验证码
	private List<Employee> userList;
	private List<Role> roleList=new ArrayList<Role>();
	private List<Integer> rids=new ArrayList<Integer>();;
	
	private RoleService roleService;
	
	
	public List<Employee> getUserList() {
		return userList;
	}

	public void setUserList(List<Employee> userList) {
		this.userList = userList;
	}

	public List<Role> getRoleList() {
		return roleList;
	}

	public void setRoleList(List<Role> roleList) {
		this.roleList = roleList;
	}

	public List<Integer> getRids() {
		return rids;
	}

	public void setRids(List<Integer> rids) {
		this.rids = rids;
	}

	public RoleService getRoleService() {
		return roleService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public EmployeeBiz getEmployeeBiz() {
		return employeeBiz;
	}

	public String getVerify() {
		return verify;
	}

	public void setVerify(String verify) {
		this.verify = verify;
	}

	public String getErrorMsg() {
		return errorMsg;
	}

	public void setErrorMsg(String errorMsg) {
		this.errorMsg = errorMsg;
	}

	public Employee getEmployee() {
		return employee;
	}

	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

	public void setEmployeeBiz(EmployeeBiz employeeBiz) {
		this.employeeBiz = employeeBiz;
	}
	
	public String login(){
		
		//判断验证码是否正确
		String randStr=(String)ActionContext.getContext().getSession().get("random");
		if(!verify.equals(randStr)){
			errorMsg="验证码错误";
			return ERROR;
		}
		
		//对密码进行MD5加密并赋给employee对象
		MD5 md5 =new MD5(employee.getPassword());
		String mdpwd =md5.compute();
		System.out.println(mdpwd);
		employee.setPassword(mdpwd);
		
		//
		//Employee newEmployee=this.employeeBiz.login(employee.getSn(), employee.getPassword());	
		Employee newEmployee= this.employeeBiz.login(employee);
		if(newEmployee!=null){
			List<Integer> ridlist=employeeBiz.getRole(newEmployee);
			Map session =ActionContext.getContext().getSession();
			session.put("curruser", newEmployee);
			session.put("role", ridlist);
			return SUCCESS;
		}else{
			errorMsg="用户名或密码错误";
			return ERROR;
		}
		
	}
	// 处理operateUser请求方法
		public String operateUser() {
			userList = employeeBiz.operateUser();
			return Action.SUCCESS;
		}
		// 处理operateUR请求方法
		public String operateUR() {
			roleList = roleService.operateRole();
			return Action.SUCCESS;
		}
		// 处理newUser请求方法
		public String newUser() {
			//必须分配角色
			if(rids.size()==0){
				this.addActionError("必须分配角色");
				return "input";
			}
			if(employee.getName()==null||"".equals(employee.getName())){
				this.addActionError("必须指定用户名");
				return "input";
			}		
			MD5 md5=new MD5(employee.getPassword());
			employee.setPassword(md5.compute());
			String result =employeeBiz.newUser(employee, rids);
			if(result.equals("input")){
				this.addActionError("用户名已经存在");
				return "input";
			}
			return SUCCESS;
			
		}
		// 处理deleteUser请求方法
		public String removeUser() {
			employeeBiz.removeUser(employee);
			return SUCCESS;
		}
		// 处理findSingleUser请求方法
		public String findSingleUser() {
			//获取用户信息
			employee = employeeBiz.findSingleUser(employee);
			return SUCCESS;
		}
		// 处理modifyUser请求方法
		public String modifyUser() {
			if(employee.getName()==null||"".equals(employee.getName())){
				this.addActionError("必须指定用户名");
				return "input";
			}		
			MD5 md5=new MD5(employee.getPassword());
			employee.setPassword(md5.compute());
			employeeBiz.modifyUser(employee);	
			return SUCCESS;
		}
		// 处理listRole请求方法
		public String listRole() {
			//当前用户的角色
			ServletActionContext.getRequest().setAttribute("had", employeeBiz.getRole(employee));
			//当前用户信息
			HttpSession hs = ServletActionContext.getRequest().getSession();
			if (employee != null) {
				hs.setAttribute("user", employee);
			}
			//所有的角色信心
			roleList = roleService.operateRole();
			return Action.SUCCESS;
		}
		// 处理changeUR请求方法
		public String changeUR() {
			HttpSession hs = ServletActionContext.getRequest().getSession();
			return employeeBiz.changeUR(rids, (Employee) hs.getAttribute("user"));
		}
		public String exit(){
			HttpSession hs = ServletActionContext.getRequest().getSession();
			hs.invalidate();
			return SUCCESS;
		}
}
